package cn.com.KnowNews.servlet;

import cn.com.KnowNews.utils.DBConn;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "CheckAdminServlet",urlPatterns = "/checkAdmin")
public class CheckAdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn= DBConn.getConnection();
        String addusername = request.getParameter("addusername");
      // System.out.println(addusername);
        String sql = "select * from t_admin where username=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,addusername);
            ResultSet rs = ps.executeQuery();
           if(rs.next()){
               response.getWriter().println("该用户名不可用！");
               response.sendRedirect("");
           }else{
               response.getWriter().println("该用户名可以使用");
           }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
